import { execute } from "../../database/connector";

import { ProjectQueries } from "../queries/project.query";
import { IProject, IMaxProject } from "../../models/project.model";

/**
 * gets active projects
 */
export const getProjects = async () => {
  return execute<IProject[]>(ProjectQueries.GetProjects, []);
};

/**
* gets mid projectChecks
*/
export const getMidProjectChecks = async () => {
  return execute<IProject[]>(ProjectQueries.GetMidProjectChecks, []);
 };
/**
 * gets subject projectChecks
 */
export const getSubjectProjectChecks = async () => {
    console.log('getSubjectProjectChecks')
    return execute<IProject[]>(ProjectQueries.GetSubjectProjectChecks, []);
};

/**
* gets final projectChecks
*/
export const getFinalProjectChecks = async () => {
 return execute<IProject[]>(ProjectQueries.GetFinalProjectChecks, []);
};

/**
 * gets active mid projects
 */
export const getNotifiedMidProjects = async () => {
  return execute<IProject[]>(ProjectQueries.GetNotifiedMidProjects, []);
};

/**
 * gets active mid projects
 */
export const getNotifiedFinalProjects = async () => {
  return execute<IProject[]>(ProjectQueries.GetNotifiedFinalProjects, []);
};


/**
 * gets a project based on id provided
 */
export const getProjectById = async (id: IProject['id']) => {
  return execute<IProject>(ProjectQueries.GetProjectById, [id]);
};

/**
 * gets a project based on group id provided
 */
export const GetProjectsByGroupId = async (id: IProject['id']) => {
  return execute<IProject>(ProjectQueries.GetProjectsByGroupId, [id]);
};

/**
 * gets a project based on project id provided
 */
export const getProjectByProjectId = async (projectId: IProject['projectId']) => {
  return execute<IProject[]>(ProjectQueries.GetProjectByProjectId, [projectId]);
};

/**
 * gets a project based on status provided
 */
export const getProjectsByStatus = async (projectStatus: IProject['projectStatus'][]) => {
    console.log('------getProjectsByStatus------',projectStatus)
  return execute<IProject[]>(ProjectQueries.GetProjectsByStatus, [[projectStatus]]);
};

/**
 * gets a project based on group ids provided
 */
export const getProjectsByGroupIds = async (groupIds: IProject['projectGroupId'][]) => {
  return execute<IProject[]>(ProjectQueries.GetProjectsByGroupIds, [[groupIds]]);
};

/**
* gets a project based on owner provided
*/
export const getProjectsByOwnerId = async (projectOwnerId: IProject['projectOwnerId']) => {
 return execute<IProject[]>(ProjectQueries.GetProjectsByOwnerId, [projectOwnerId]);
};

/**
* gets a project based on owner provided
*/
export const getProjectsByApplicantId = async (projectApplicantId: IProject['projectApplicantId']) => {
  return execute<IProject[]>(ProjectQueries.GetProjectsByApplicantId, [projectApplicantId]);
 };

 /**
* gets a project based on owner provided
*/
export const getProjectsByParticipantId = async (projectParticipantId: IProject['projectOwnerId']) => {
  return execute<IProject[]>(ProjectQueries.GetProjectsByParticipantId, [projectParticipantId, projectParticipantId]);
 };

/**
* gets the last project
*/
export const getLastProject = async () => {
  return execute<IMaxProject[]>(ProjectQueries.GetLastProject, []);
 };

/**
 * adds a new active project record
 */
export const insertProject = async (project: IProject) => {
    const keys = Object.keys(project).join(',');
    const sql = `
    INSERT INTO my_schema.project (${keys})
      VALUES ?
    `
  const result = await execute<{ affectedRows: number }>(sql, [
    [Object.values(project)]
  ]);
  // 自己写的mysql插入语句
  return result.affectedRows > 0;
};

/**
 * updates project information based on the id provided
 */
export const updateProject = async (project: IProject) => {
    let keys = '';
    Object.keys(project).filter(key => key !== 'id'&& key !== 'projectId').forEach((key, index) => {
        // @ts-ignore
        keys += `${key} = '${project[key]}'`
        if (index < Object.keys(project).filter(key => key !== 'id'&& key !== 'projectId').length - 1) {
            keys += ','
        }
    })
    // 编写sql语句更新
    const sql = `
    UPDATE my_schema.project
    SET ${keys}
    WHERE id = ${project.id}
    `

  const result = await execute<{ affectedRows: number }>(ProjectQueries.UpdateProjectById, [project, project.id]);
  return result.affectedRows > 0;
};

/**
 * updates project status based on the id provided
 */
export const updateProjectStatus = async (projectStatus: string, projectId: string) => {
  const result = await execute<{ affectedRows: number }>(ProjectQueries.UpdateProjectStatusById, [projectStatus, projectId]);
  return result.affectedRows > 0;
};

/**
 * updates project cost based on the id provided
 */
export const updateProjectCost = async (project: IProject) => {
  const result = await execute<{ affectedRows: number }>(ProjectQueries.UpdateProjectById, [project.projectStatus, project.projectApprovedCost, project.id]);
  return result.affectedRows > 0;
};

/**
 * updates project information based on the id provided
 */
 export const deleteProject = async (id: IProject['id']) => {
  const result = await execute<{ affectedRows: number }>(ProjectQueries.DeleteProjectById, [
    id
  ]);
  return result.affectedRows > 0;
};